using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;

namespace SV2Xml {
    partial class AboutBox : Form {
        public AboutBox() {
            InitializeComponent();

            TreeNode baseNode = this.treeView1.Nodes[0];
            baseNode.Expand();
            foreach (AssemblyName name in this.LoadedAssemblies) {
                TreeNode node = new TreeNode(this.GetAssemblyTitle(name));
                node.Nodes.Add(new TreeNode(string.Format("Product: {0}", this.GetAssemblyProduct(name))));
                node.Nodes.Add(new TreeNode(string.Format("Version: {0}", this.GetAssemblyVersion(name))));
                node.Nodes.Add(new TreeNode(string.Format("Copyright: {0}", this.GetAssemblyCopyright(name))));
                node.Nodes.Add(new TreeNode(string.Format("Company: {0}", this.GetAssemblyCompany(name))));
                node.Nodes.Add(new TreeNode(string.Format("Description: {0}", this.GetAssemblyDescription(name))));
                baseNode.Nodes.Add(node);
            }
        }

        private List<AssemblyName> LoadedAssemblies {
            get {
                List<AssemblyName> assemblies = new List<AssemblyName>();
                assemblies.Add(Assembly.GetExecutingAssembly().GetName());

                foreach (AssemblyName a in Assembly.GetExecutingAssembly().GetReferencedAssemblies()) {
                    assemblies.Add(a);
                }
                
                return assemblies;
            }
        }

        #region Assembly Attribute Accessors

        private string GetAssemblyTitle(AssemblyName name) {
            // Get all Title attributes on this assembly
            object[] attributes = Assembly.Load(name).GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
            // If there is at least one Title attribute
            if (attributes.Length > 0) {
                // Select the first one
                AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                // If it is not an empty string, return it
                if (titleAttribute.Title != "")
                    return titleAttribute.Title;
            }
            // If there was no Title attribute, or if the Title attribute was the empty string, return the .exe name
            return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
        }

        private string GetAssemblyVersion(AssemblyName name) {
            return Assembly.Load(name).GetName().Version.ToString();
        }

        private string GetAssemblyDescription(AssemblyName name) {
            // Get all Description attributes on this assembly
            object[] attributes = Assembly.Load(name).GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
            // If there aren't any Description attributes, return an empty string
            if (attributes.Length == 0)
                return "";
            // If there is a Description attribute, return its value
            return ((AssemblyDescriptionAttribute)attributes[0]).Description;
        }

        private string GetAssemblyProduct(AssemblyName name) {
            // Get all Product attributes on this assembly
            object[] attributes = Assembly.Load(name).GetCustomAttributes(typeof(AssemblyProductAttribute), false);
            // If there aren't any Product attributes, return an empty string
            if (attributes.Length == 0)
                return "";
            // If there is a Product attribute, return its value
            return ((AssemblyProductAttribute)attributes[0]).Product;
        }

        private string GetAssemblyCopyright(AssemblyName name) {
            // Get all Copyright attributes on this assembly
            object[] attributes = Assembly.Load(name).GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
            // If there aren't any Copyright attributes, return an empty string
            if (attributes.Length == 0)
                return "";
            // If there is a Copyright attribute, return its value
            return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
        }

        private string GetAssemblyCompany(AssemblyName name) {
            // Get all Company attributes on this assembly
            object[] attributes = Assembly.Load(name).GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
            // If there aren't any Company attributes, return an empty string
            if (attributes.Length == 0)
                return "";
            // If there is a Company attribute, return its value
            return ((AssemblyCompanyAttribute)attributes[0]).Company;
        }
        #endregion

        private void okButton_Click(object sender, EventArgs e) {
            this.Close();
        }
    }
}
